package ${packageName}<#if moduleName?exists><#if moduleName!=''>.${moduleName}</#if></#if>.controller;

import cn.jjxx.core.common.entity.tree.BootstrapTreeHelper;
import cn.jjxx.core.common.entity.tree.BootstrapTreeNode;
import cn.jjxx.core.common.entity.tree.TreeSortUtil;
import cn.jjxx.core.common.data.DuplicateValid;
import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.model.PageJson;
import cn.jjxx.core.model.ValidJson;
import cn.jjxx.core.query.annotation.PageableDefaults;
import cn.jjxx.core.query.data.PropertyPreFilterable;
import cn.jjxx.core.query.data.QueryPropertyPreFilter;
import cn.jjxx.core.query.data.Queryable;
import cn.jjxx.core.query.utils.QueryableConvertUtils;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresMethodPermissions;
import cn.jjxx.core.utils.JeewebPropertiesUtil;
import cn.jjxx.core.utils.MessageUtils;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.annotations.TableName;

import org.framework.superutil.thirdparty.excel.ExcelDataFormatter;
import org.framework.superutil.thirdparty.excel.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import cn.jjxx.core.common.controller.BaseBeanController;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresPathPermission;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.framework.superutil.thirdparty.excel.ExcelUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import cn.jjxx.modules.common.bean.UploadExcel;
import cn.jjxx.modules.sys.entity.User;
import cn.jjxx.modules.sys.utils.UserUtils;
import cn.jjxx.core.config.BillConfig;
import ${packageName}<#if moduleName?exists><#if moduleName!=''>.${moduleName}</#if></#if>.entity.${entityName?cap_first};
import ${packageName}<#if moduleName?exists><#if moduleName!=''>.${moduleName}</#if></#if>.service.I${entityName?cap_first}Service;
import cn.jjxx.modules.map.service.ICommonMapService;

/**   
 * @Title: ${functionName}
 * @Description: ${functionDesc}
 * @author ${functionAuthor}
 * @date ${time}
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("${r'${admin.url.prefix}'}/${moduleName}/${entityName?lower_case}")
@RequiresPathPermission("${moduleName}:${entityName?lower_case}")
public class ${entityName?cap_first}Controller extends BaseBeanController<${entityName?cap_first}> {

    @Autowired
    protected I${entityName?cap_first}Service ${entityName?uncap_first}Service;
    @Autowired
    protected ICommonMapService commonMapService;

    public ${entityName?cap_first} get(String id) {
        if (!ObjectUtils.isNullOrEmpty(id)) {
            return ${entityName?uncap_first}Service.selectById(id);
        } else {
            return newModel();
        }
    }

	/**
	 * @Description: 根据获取${functionName}的实体数据 .<br>
	 * @param id 主键Id.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequiresMethodPermissions("list")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("list");
    }

	/**
	 * @Description: 根据查询条件动态加载${functionName}的树数据 .<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
	@ResponseBody
	@RequestMapping(value = "treeData")
	public void treeData(Queryable queryable,
			@RequestParam(value = "nodeid", required = false, defaultValue = "") String nodeid,
			@RequestParam(value = "async", required = false, defaultValue = "false") boolean async,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		EntityWrapper<${entityName?cap_first}> entityWrapper = new EntityWrapper<${entityName?cap_first}>(entityClass);
		entityWrapper.setTableAlias("t.");
		List<${entityName?cap_first}> treeNodeList = null;
		if (!async) { // 非异步 查自己和子子孙孙
			treeNodeList = ${entityName?uncap_first}Service.selectTreeList(queryable, entityWrapper);
			TreeSortUtil.create().sort(treeNodeList).async(treeNodeList);
		} else { // 异步模式只查自己
			// queryable.addCondition("parentId", nodeid);
			if (ObjectUtils.isNullOrEmpty(nodeid)) {
				// 判断的应该是多个OR条件
				entityWrapper.isNull("parentId");
			} else {
				entityWrapper.eq("parentId", nodeid);
			}
			treeNodeList = ${entityName?uncap_first}Service.selectTreeList(queryable, entityWrapper);
			TreeSortUtil.create().sync(treeNodeList);
		}
		PropertyPreFilterable propertyPreFilterable = new QueryPropertyPreFilter();
		propertyPreFilterable.addQueryProperty("id", "name", "expanded", "hasChildren", "leaf", "loaded", "level",
				"parentId");
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		PageJson<${entityName?cap_first}> pagejson = new PageJson<${entityName?cap_first}>(treeNodeList);
		String content = JSON.toJSONString(pagejson, filter);
		StringUtils.printJson(response, content);
	}

	/**
	 * @Description: 根据页码和每页记录数，以及查询条件动态加载${functionName}的数据 .<br>
	 * @param nodeid 节点Id.<br>
	 * @param async 是否异步.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
	@RequestMapping(value = "ajaxTreeList", method ={ RequestMethod.GET, RequestMethod.POST })
	private void ajaxTreeList(Queryable queryable,
			@RequestParam(value = "nodeid", required = false, defaultValue = "") String nodeid,
			@RequestParam(value = "async", required = false, defaultValue = "false") boolean async,
			HttpServletRequest request, HttpServletResponse response, PropertyPreFilterable propertyPreFilterable)
			throws IOException {
		EntityWrapper<${entityName?cap_first}> entityWrapper = new EntityWrapper<${entityName?cap_first}>(entityClass);
		entityWrapper.setTableAlias("t.");
		List<${entityName?cap_first}> treeNodeList = null;
		if (!async) { // 非异步 查自己和子子孙孙
			treeNodeList = ${entityName?uncap_first}Service.selectTreeList(queryable, entityWrapper);
			TreeSortUtil.create().sort(treeNodeList).async(treeNodeList);
		} else { // 异步模式只查自己
			// queryable.addCondition("parentId", nodeid);
			if (ObjectUtils.isNullOrEmpty(nodeid)) {
				// 判断的应该是多个OR条件
				entityWrapper.isNull("parentId");
			} else {
				entityWrapper.eq("parentId", nodeid);
			}
			treeNodeList = ${entityName?uncap_first}Service.selectTreeList(queryable, entityWrapper);
			TreeSortUtil.create().sync(treeNodeList);
		}
		propertyPreFilterable.addQueryProperty("id", "expanded", "hasChildren", "leaf", "loaded", "level", "parentId");
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		PageJson<${entityName?cap_first}> pagejson = new PageJson<${entityName?cap_first}>(treeNodeList);
		String content = JSON.toJSONString(pagejson, filter);
		StringUtils.printJson(response, content);
	}

	/**
	 * @Description: 根据查询条件动态加载${functionName}的适合于bootstrapTree的数据 .<br>
	 * @param nodeid 节点Id.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
	@RequestMapping(value = "bootstrapTreeData")
	private void bootstrapTreeData(Queryable queryable,
			@RequestParam(value = "nodeid", required = false, defaultValue = "") String nodeid, HttpServletRequest request,
			HttpServletResponse response, PropertyPreFilterable propertyPreFilterable) throws IOException {
		EntityWrapper<${entityName?cap_first}> entityWrapper = new EntityWrapper<${entityName?cap_first}>(entityClass);
		entityWrapper.setTableAlias("t.");
		List<${entityName?cap_first}> treeNodeList = ${entityName?uncap_first}Service.selectTreeList(queryable, entityWrapper);
		List<BootstrapTreeNode> bootstrapTreeNodes = BootstrapTreeHelper.create().sort(treeNodeList);
		propertyPreFilterable.addQueryProperty("text", "href", "tags", "nodes");
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		String content = JSON.toJSONString(bootstrapTreeNodes, filter);
		StringUtils.printJson(response, content);
	}

	<#if generatorActions?seq_contains("create")>
	/**
	 * @Description: 跳转至${functionName}的添加界面 .<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response) {
        if (!model.containsAttribute("data")) {
            model.addAttribute("data", newModel());
        }
        return display("edit");
    }
    
	/**
	 * @Description: 新增${functionName}的数据 .<br>
	 * @param ${entityName?uncap_first} ${functionName}实体类.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson create(Model model, @Valid @ModelAttribute("data") ${entityName?cap_first} ${entityName?uncap_first}, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        return doSave(${entityName?uncap_first}, request, response, result);
    }
    </#if>

	<#if generatorActions?seq_contains("update")>
	/**
	 * @Description: 跳转至${functionName}的更新界面 .<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(@PathVariable("id") String id, Model model, HttpServletRequest request,
                              HttpServletResponse response) {
        ${entityName?cap_first} ${entityName?uncap_first} = get(id);
        model.addAttribute("data", ${entityName?uncap_first});
        return display("edit");
    }

	/**
	 * @Description: 修改${functionName}的数据 .<br>
	 * @param ${entityName?uncap_first} ${functionName}实体类.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "{id}/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson update(Model model, @Valid @ModelAttribute("data") ${entityName?cap_first} ${entityName?uncap_first}, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        return doSave(${entityName?uncap_first}, request, response, result);
    }
    </#if>

	/**
	 * @Description: 执行保存${functionName}的数据操作 .<br>
	 * @param ${entityName?uncap_first} ${functionName}实体类.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson doSave(${entityName?cap_first} ${entityName?uncap_first}, HttpServletRequest request, HttpServletResponse response,
                           BindingResult result) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("保存成功");
        if (hasError(${entityName?uncap_first}, result)) {
            // 错误提示
            String errorMsg = errorMsg(result);
            if (!StringUtils.isEmpty(errorMsg)) {
                ajaxJson.fail(errorMsg);
            } else {
                ajaxJson.fail("保存失败");
            }
            return ajaxJson;
        }
        try {
            if (StringUtils.isEmpty(${entityName?uncap_first}.getId())) {
                ${entityName?uncap_first}Service.insert(${entityName?uncap_first});
            } else {
                ${entityName?uncap_first}Service.insertOrUpdate(${entityName?uncap_first});
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("保存失败!<br />原因:" + e.getMessage());
        }
        return ajaxJson;
    }

	<#if generatorActions?seq_contains("delete")>
	/**
	 * @Description: 单条删除${functionName} .<br>
	 * @param id 单据Id.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson delete(@PathVariable("id") String id) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("删除成功");
        TableName table = ${entityName?cap_first}.class.getAnnotation(TableName.class);
    	String tableName = table.value();
		String dbName = MessageUtils.getMessage("connection.dbname");
        try {
        	List<String> ids = Arrays.asList(id.split(","));
        	//得到被其他表引用的结果
			String quoteMsg = commonMapService.quoteMsg(dbName, tableName, ids);
			if(StringUtils.isNotEmpty(quoteMsg)){
				ajaxJson.setMsg("删除失败,"+quoteMsg);
				return ajaxJson;
			}
            ${entityName?uncap_first}Service.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("删除失败");
        }
        return ajaxJson;
    }

	/**
	 * @Description: 执行保存${functionName}的数据操作 .<br>
	 * @param ids 单据Id集合.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "batch/delete", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson batchDelete(@RequestParam(value = "ids", required = false) String[] ids) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("批量删除成功");
        TableName table = ${entityName?cap_first}.class.getAnnotation(TableName.class);
    	String tableName = table.value();
		String dbName = MessageUtils.getMessage("connection.dbname");
        try {
            List<String> idList = java.util.Arrays.asList(ids);
            //得到被其他表引用的结果
			String quoteMsg = commonMapService.quoteMsg(dbName, tableName, idList);
			if(StringUtils.isNotEmpty(quoteMsg)){
				ajaxJson.setMsg("批量删除失败,"+quoteMsg);
				return ajaxJson;
			}
            ${entityName?uncap_first}Service.deleteBatchIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("批量删除失败");
        }
        return ajaxJson;
    }
    </#if>

	/**
	 * @Description: 跳转至${functionName}的查看界面 .<br>
	 * @param ids 单据Id.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String view(Model model, @PathVariable("id") String id, HttpServletRequest request,
                       HttpServletResponse response) {
        ${entityName?cap_first} ${entityName?uncap_first} = get(id);
        model.addAttribute("data", ${entityName?uncap_first});
        return display("edit");
    }

	/**
	 * @Description: 验证${functionName}功能数据的唯一性 .<br>
	 * @param duplicateValid 校验实体类.<br>
	 * @author ${functionAuthor} .<br>
	 * @date ${time}.<br>
	 */
    @RequestMapping(value = "validate", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public ValidJson validate(DuplicateValid duplicateValid, HttpServletRequest request) {
        ValidJson validJson = new ValidJson();
        Boolean valid = Boolean.FALSE;
        try {
            EntityWrapper<${entityName?cap_first}> entityWrapper = new EntityWrapper<${entityName?cap_first}>(entityClass);
            valid = ${entityName?uncap_first}Service.doValid(duplicateValid,entityWrapper);
            if (valid) {
                validJson.setStatus("y");
                validJson.setInfo("验证通过!");
            } else {
                validJson.setStatus("n");
                if (!StringUtils.isEmpty(duplicateValid.getErrorMsg())) {
                    validJson.setInfo(duplicateValid.getErrorMsg());
                } else {
                    validJson.setInfo("当前信息重复!");
                }
            }
        } catch (Exception e) {
            validJson.setStatus("n");
            validJson.setInfo("验证异常，请检查字段是否正确!");
        }
        return validJson;
    }
    
    <#if generatorActions?seq_contains("impexcel")>
    /**
     * @Description: 导入${functionName}的excel数据 .<br>
     * @param model 模型实体类.<br>
     * @param excel excel参数实体类 .<br>
     * @param request http请求 .<br>
     * @param response http响应.<br>
     * @return AjaxJson 返回导入结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @RequestMapping(value = "impexcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson impExcel(Model model,UploadExcel excel,HttpServletRequest request, HttpServletResponse response) {
    	AjaxJson j = new AjaxJson();
    	response.setContentType("text/plain");
    	j.setMsg("导入成功");
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) { // 判断request是否有文件上传
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator<String> ite = multiRequest.getFileNames();
			while (ite.hasNext()) {
				try {
					MultipartFile file = multiRequest.getFile(ite.next());
					CommonsMultipartFile cFile = (CommonsMultipartFile) file;
			        DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();
			        InputStream inputStream = fileItem.getInputStream();
					List<${entityName?cap_first}> list = new ExcelUtils<${entityName?cap_first}>(new ${entityName?cap_first}()).readFromFile(null, inputStream);
					${entityName?uncap_first}Service.insertBatch(list);
				}catch(Exception e){
					e.printStackTrace();
					j.setRet(AjaxJson.RET_FAIL);
					j.setMsg("导入失败");
				}
			}
		}
		return j;
    }
    </#if>
    
    <#if generatorActions?seq_contains("expexcelmodel")>
    /**
     * @Description: 导出${functionName}的EXCEL模型 .<br>
     * @param request http请求.<br>
     * @param response http响应.<br>   
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @RequestMapping(value = "expexcelmodel", method = RequestMethod.GET)
    public void expExcelModel(HttpServletRequest request, HttpServletResponse response) {
    	ExcelUtils.setIsExportTemplate(true);
    	ExcelUtils.setFirstTitle("${functionName}模板");
    	ExcelUtils.setSecTitle("导出人："+UserUtils.getUser().getRealname());
    	ExcelUtils.downloadXlsx(response, null, ${entityName?cap_first}.class, null, "${functionName}模板");
    }
    </#if>
    
    <#if generatorActions?seq_contains("expexcel")>
    /**
     * @Description: 导出${functionName}的EXCEL .<br>
     * @param request http请求.<br>
     * @param response http响应.<br>   
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @RequestMapping(value = "expexcel", method = RequestMethod.GET)
    public void expExcel(HttpServletRequest request, HttpServletResponse response) {
    	EntityWrapper<${entityName?cap_first}> wrapper = new EntityWrapper<${entityName?cap_first}>(${entityName?cap_first}.class);
    	wrapper.setTableAlias("t.");
    	List<${entityName?cap_first}> list = ${entityName?uncap_first}Service.selectList(wrapper);
    	ExcelUtils.setIsExportTemplate(false);
    	ExcelUtils.setFirstTitle("${functionName}");
    	ExcelUtils.setSecTitle("导出人："+UserUtils.getUser().getRealname());
    	ExcelUtils.downloadXlsx(response, list, ${entityName?cap_first}.class, null, "${functionName}");
    }
    </#if>
    
    <#if generatorActions?seq_contains("submit")>
    /**
     * @Description: 批量提交数据.<br>
     * @param request http请求.<br>   
     * @param ids 传入的id集合.<br>   
     * @return AjaxJson 处理结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @ResponseBody
	@RequestMapping(value = "submit")
    public AjaxJson batchSubmit(HttpServletRequest request,String[] ids){
    	AjaxJson j = new AjaxJson();
    	j.setMsg("批量提交成功");
    	List<String> bills = Arrays.asList(ids);
    	try {
    		bills = getAllowOptBills(bills, String.valueOf(BillConfig.SAVE));
    		List<${entityName?cap_first}> entityList = setBacthUpdate(bills, String.valueOf(BillConfig.SUBMIT));
    		${entityName?uncap_first}Service.updateBatch(entityList);
    	} catch (Exception e) {
			j.setMsg("批量提交失败,数据已被其他单据使用");
			e.printStackTrace();
		}
		return j;
    }
    </#if>
    
    <#if generatorActions?seq_contains("review")>
    /**
     * @Description: 批量审核数据.<br>
     * @param request http请求.<br>   
     * @param ids 传入的id集合.<br>   
     * @return AjaxJson 处理结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @ResponseBody
	@RequestMapping(value = "review")
    public AjaxJson batchReview(HttpServletRequest request,String[] ids){
    	AjaxJson j = new AjaxJson();
    	j.setMsg("批量审核成功");
    	List<String> bills = Arrays.asList(ids);
    	try {
    		bills = getAllowOptBills(bills, String.valueOf(BillConfig.SUBMIT));
    		List<${entityName?cap_first}> entityList = setBacthUpdate(bills, String.valueOf(BillConfig.REVIEW));
    		${entityName?uncap_first}Service.updateBatch(entityList);
    	} catch (Exception e) {
			j.setMsg("批量审核失败,数据已被其他单据使用");
			e.printStackTrace();
		}
		return j;
    }
    </#if>
    
    <#if generatorActions?seq_contains("unreview")>
    /**
     * @Description: 批量反审核数据.<br>
     * @param request http请求.<br>   
     * @param ids 传入的id集合.<br>   
     * @return AjaxJson 处理结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @ResponseBody
	@RequestMapping(value = "unreview")
    public AjaxJson batchUnreview(HttpServletRequest request,String[] ids){
    	AjaxJson j = new AjaxJson();
    	j.setMsg("批量反审核成功");
    	List<String> bills = Arrays.asList(ids);
    	try {
    		bills = getAllowOptBills(bills, String.valueOf(BillConfig.REVIEW));
    		List<${entityName?cap_first}> entityList = setBacthUpdate(bills, String.valueOf(BillConfig.SAVE));
    		${entityName?uncap_first}Service.updateBatch(entityList);
    	} catch (Exception e) {
			j.setMsg("批量反审核失败,数据已被其他单据使用");
			e.printStackTrace();
		}
		return j;
    }
    </#if>
    
    <#if generatorActions?seq_contains("enabled")>
    /**
     * @Description: 批量启用数据.<br>
     * @param request http请求.<br>   
     * @param ids 传入的id集合.<br>   
     * @return AjaxJson 处理结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @ResponseBody
	@RequestMapping(value = "enabled")
    public AjaxJson batchEnabled(HttpServletRequest request,String[] ids){
    	AjaxJson j = new AjaxJson();
    	j.setMsg("批量启用成功");
    	List<String> bills = Arrays.asList(ids);
    	try {
    		bills = getAllowOptBills(bills, String.valueOf(BillConfig.DISABLED));
    		List<${entityName?cap_first}> entityList = setBacthUpdate(bills, String.valueOf(BillConfig.ENABLED));
    		${entityName?uncap_first}Service.updateBatch(entityList);
    	} catch (Exception e) {
			j.setMsg("批量启用失败,数据已被其他单据使用");
			e.printStackTrace();
		}
		return j;
    }
    </#if>
    
    <#if generatorActions?seq_contains("unabled")>
    /**
     * @Description: 批量禁用核数据.<br>
     * @param request http请求.<br>   
     * @param ids 传入的id集合.<br>   
     * @return AjaxJson 处理结果.<br> 
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    @ResponseBody
	@RequestMapping(value = "unabled")
    public AjaxJson batchDisabled(HttpServletRequest request,String[] ids){
    	AjaxJson j = new AjaxJson();
    	j.setMsg("批量禁用成功");
    	List<String> bills = Arrays.asList(ids);
    	try {
    		bills = getAllowOptBills(bills, String.valueOf(BillConfig.ENABLED));
    		List<${entityName?cap_first}> entityList = setBacthUpdate(bills, String.valueOf(BillConfig.DISABLED));
    		${entityName?uncap_first}Service.updateBatch(entityList);
    	} catch (Exception e) {
			j.setMsg("批量禁用失败,数据已被其他单据使用");
			e.printStackTrace();
		}
		return j;
    }
    </#if>
    
    /**
     * @Description: 得到可以被审核的单据 .<br>
     * @param bills 单据Id集合.<br>
     * @param status 可操作状态.<br>
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    private List<String> getAllowOptBills(List<String> bills,String status){
    	EntityWrapper<${entityName?cap_first}> wrapper = new EntityWrapper<${entityName?cap_first}>();
    	wrapper.in("id", bills);
    	wrapper.eq("status", status);
    	List<${entityName?cap_first}> list = ${entityName?uncap_first}Service.selectList(wrapper);
    	bills = new ArrayList<String>();
    	for(${entityName?cap_first} bean:list){
    		bills.add(bean.getId());
    	}
    	return bills;
    }
    
    /**
     * @Description: 设置批量更新参数 .<br>
     * @param bills 可以操作的Id集合.<br>
     * @param newStatus 被修改的状态设置值.<br>
     * @author ${functionAuthor} .<br>
     * @date ${time} .<br>
     */
    private List<${entityName?cap_first}> setBacthUpdate(List<String> bills,String newStatus){
    	List<${entityName?cap_first}> list = new ArrayList<${entityName?cap_first}>();
    	User user = UserUtils.getUser();
    	if(bills.size()>0){
    		list = ${entityName?uncap_first}Service.selectBatchIds(bills);
        	for(${entityName?cap_first} bean:list){
        		bean.setStatus(Integer.valueOf(newStatus));
        		if(newStatus.equals(String.valueOf(BillConfig.SAVE))||
        				newStatus.equals(String.valueOf(BillConfig.SUBMIT))){
        		}
        	}
    	}
    	return list;
    }
}
